<cfcomponent output="false">
	<cfset this.applicationholder 	= 0 />
	<cfset this.applicationname 	= "" />
	<cfset this.applicationscope 	= "" />
	<cfset this.xmlhelper 			= createObject("component", "%newApplicationDirectoryDotPath%.lib.tools.convert.Xml") />
	<cfset this.tablename 			= "" />
	<cfset this.outerxmlwrapper 	= "out" />
	<cfset this.innerxmlwrapper 	= "in" />
	<cfset this.applicationname 	= "" />
	<cfset this.primarykey 			= "id" />
	
	<cffunction name="getApplication" returntype="struct" output="false">
		<cfargument name="appName" type="string" required="true">
		<cfset var myapp = "" />
		<cfscript>		
			var reseturl = "";
			reseturl = getCurrentTemplatePath();
			reseturl = replace(reseturl, expandpath( "/"), "");
			reseturl = rereplace(reseturl, "(.*)(\\service.*)","\1");
			reseturl = replace(reseturl, "\","/", "ALL");
			reseturl = "http://#cgi.HTTP_HOST#/#reseturl#/";
		</cfscript>
		<cflog file="wsbaseclass" text="the rest url: #reseturl#"/>
		<cflock scope="server" timeout="30" type="exclusive">
			<cfif not isObject(this.applicationholder)>
				<cftry>
					<cflog file="wsbaseclass" text="applicationholder is NOT set, calling #reseturl#"/>
					<cfscript>
						// if there is an error here, the Application scope was never created for the webservice
						this.applicationholder = grabApplicationFromTracker(arguments.appName);
					</cfscript>
					<cfcatch type="any">
						<cfhttp method="get" url="#reseturl#"/>
						<cfscript>
							// it wasn't loaded the first time, so the cfhttp takes care of that so we can grab the 
							// application scope once again
							this.applicationholder = grabApplicationFromTracker(arguments.appName);
						</cfscript>
					</cfcatch>
				</cftry>
			<cfelse>
				<cflog file="wsbaseclass" text="applicationholder is already set for #this.applicationname#"/>
				<cfset myapp = this.applicationholder>
			</cfif>
		</cflock>
		<cfif not isdefined("myapp")>
			<cfoutput>Error: Can't find the application.cfc name #this.applicationname# in the data tier service file.</cfoutput><cfabort>
		</cfif>
		<cfreturn this.applicationholder/>
	</cffunction>

	<cffunction name="grabApplicationFromTracker" returntype="struct" output="false">
		<cfargument name="appName" type="string" required="true">
		<cfset var oApp 	= createObject("java","coldfusion.runtime.ApplicationScopeTracker") />
		<cfset var myApp 	= "" />
		<cflock scope="server" timeout="10" type="exclusive">
			<cfset 	myApp = oApp.getApplicationScope(arguments.appName) />
		</cflock>
		<cfreturn myApp/>
	</cffunction>
		
	<cffunction name="getTransfer" returntype="struct" output="false">
		<cfreturn getApplication(this.applicationname).transferFactory.getTransfer() />  
	</cffunction>
	
	<cffunction name="get" returntype="query" access="remote" output="false">
		<cfargument name="id" type="string" required="yes">
		<cfscript>
			var transfer= 0;
			var onerow	= 0;
			transfer 	= getTransfer();
			onerow 		= transfer.get(this.tablename, iif(isnumeric(arguments.id), arguments.id, -1) );
		</cfscript>
		<cfreturn this.xmlhelper.structToQuery( onerow.getMemento() , this.outerxmlwrapper , this.innerxmlwrapper ) />
	</cffunction>
	
	<cffunction name="getView" returntype="query" access="remote" output="false">
		<cfargument name="id" type="numeric" required="yes">
		<cfscript>
			var transfer= 0;
			var onerow	= 0;
			transfer 	= getTransfer();
			onerow 		= transfer.get(this.tablename, arguments.id);
		</cfscript>
		<cfreturn this.xmlhelper.structToXML( onerow.getMemento() , this.outerxmlwrapper , this.innerxmlwrapper ) />
	</cffunction>
	
	<cffunction name="listAll" returntype="query" access="remote" output="false">
		<cfargument name="orderby" 	type="string" 	required="no" default="">
		<cfargument name="sort" 	type="boolean" 	required="no" default="true">
		<cfscript>
			var transfer= 0;
			var list	= 0;
			transfer 	= getTransfer();
			list 		= transfer.list(this.tablename, arguments.orderby, arguments.sort);
		</cfscript>
		<cfreturn list/>
	</cffunction>
	
	<cffunction name="listByProperty" returntype="query" access="remote" output="false">
		<cfargument name="fieldname" type="string" required="yes">
		<cfargument name="fieldvalue" type="string" required="yes">
		<cfscript>
			var transfer= 0;
			var list	= 0;
			// get transfer instance
			transfer 	= getTransfer();
			// load project by project code
			list 		= transfer.listByProperty(this.tablename, arguments.fieldname , arguments.fieldvalue );
		</cfscript>
		<!--- needs to be xml to return via webservices, convert from struct to xml --->
		<cfreturn list/>
	</cffunction>
	
	<cffunction name="listByPropertyMap" returntype="query" access="remote" output="false">
		<cfargument name="map" 			type="struct" required="yes">
		<cfargument name="orderby" 		type="string" required="no" default="id">
		<cfargument name="sort" 	   	type="string" required="no" default="asc">
		<cfscript>
			var transfer= 0;
			var list	= 0;
			// get transfer instance
			transfer 	= getTransfer();
			// load project by project code
			list 		= transfer.listByPropertyMap(this.tablename, arguments.map , arguments.orderby, arguments.sort );
		</cfscript>
		<!--- needs to be xml to return via webservices, convert from struct to xml --->
		<cfreturn list/>
	</cffunction>
			
	<cffunction name="save" returntype="any" access="remote" output="false">
		<cfargument name="xml"  type="string" required="yes" >
		<cfscript>
			var transfer 			= 0;
			var records 			= 0;
			var record 				= 0;
			var id 					= 0;
			var onerow 				= 0;
			var onepopulatedrow 	= 0;
			var thexml 				= 0;
			var i					= 0;
		</cfscript>
		<cflog file="wsbaseclass" text="#this.tablename# save xml:#arguments.xml#"/>
		<cfscript>
			thexml = arguments.xml;
			// grab the transfer instance
			transfer = getTransfer();
			thexml = replace(thexml,chr(inputbasen("19",16)),"","ALL");
			// xml query to get array of each individual countyprojects
			records = xmlsearch(thexml,"/#this.outerxmlwrapper#/#this.innerxmlwrapper#");
			// Loop thru each set of countyproject in case there were several
			for(i = 1; i lte arraylen(records); i = i + 1)
			{			
				// grab current countyproject
				record			= records[i];
				// get id
				id 				= this.xmlhelper.getXmlValue(thexml,  this.outerxmlwrapper, this.innerxmlwrapper, lcase(this.primarykey), i);
				// load project with id = id
				onerow			= transfer.get(this.tablename,id);
				// copy the xml values into a copy of the transfer gwga.count_project object
				// passing in the whole xml, the xpath to the current countyproject based off of the var "i", and the transfer object to copy the values into
				onepopulatedrow = this.xmlhelper.XmlToBean(thexml, "/#this.outerxmlwrapper#/#this.innerxmlwrapper#[#i#]", onerow);
				// save it
				transfer.save( onepopulatedrow );
			}
		</cfscript>
		<cfscript>
			if(arraylen(records) eq 1)
			{
				return trim(evaluate("numberformat(onepopulatedrow.get#this.primarykey#(),""9999"")"));
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="add" returntype="any" access="remote" output="false">
		<cfargument name="xml"  type="string" required="yes" >
		<cfset var onerow = "" />
		<cfset var onepopulatedrow = "" />
		<cfset var i = "" />
		<cfset var records = "" />
		<cfset var transfer = "" />
		<cfscript>
			// grab the transfer instance
			transfer = getTransfer();
			// xml query to get array of each individual countyprojects
			records = xmlsearch(arguments.xml,"/#this.outerxmlwrapper#/#this.innerxmlwrapper#");
			// Loop thru each set of countyproject in case there were several
			for(i = 1; i lte arraylen(records); i = i + 1)
			{			
				// grab current countyproject
				record			= records[i];
				// get id
				onerow 			= transfer.new( this.tablename );
				// copy the xml values into a copy of the transfer gwga.count_project object
				// passing in the whole xml, the xpath to the current countyproject based off of the var "i", and the transfer object to copy the values into
				onepopulatedrow = this.xmlhelper.XmlToBean( arguments.xml, "/#this.outerxmlwrapper#/#this.innerxmlwrapper#[#i#]", onerow);
				// save it
				transfer.save( onepopulatedrow );
			}
		</cfscript>
		<cfscript>
		
			if(arraylen(records) eq 1)
			{
				return evaluate("onepopulatedrow.get#this.primarykey#()");
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="delete" returntype="void" access="remote" output="false">
		<cfargument name="id" type="numeric" required="yes">
		<cfscript>
			var transfer= 0;
			var onerow	= 0;
			// get transfer instance
			transfer = getTransfer();
			// load task by id
			onerow = transfer.get( this.tablename , arguments.id );
			// resave
			transfer.delete( onerow );
			return;
		</cfscript>
	</cffunction>
	
	<cffunction name="deleteByProperty" returntype="query" access="remote" output="false">
		<cfargument name="fieldname" type="string" required="yes">
		<cfargument name="fieldvalue" type="string" required="yes">
		<cfscript>
			var transfer = 0;
			var list = 0;
			// get transfer instance
			transfer 	= getTransfer();
			// load project by project code
			list 		= transfer.listByProperty(this.tablename, arguments.fieldname , arguments.fieldvalue );
		</cfscript>
		<cfoutput query="list">
			<cfscript>
				delete( evaluate('list.#this.primarykey#') );
			</cfscript>
		</cfoutput>
		<cfreturn list/>
	</cffunction>
	
</cfcomponent>
